﻿using System;
using System.Collections.Generic;
using System.Text;
using Mesuro.Common;
using System.Windows;
using Vector = Mesuro.Common.Vector;
using System.Windows.Media;

namespace Mesuro.Graphing.Utilities {
	public class CloudPlot : Plot {
		protected PlotStyle PlotStyle;

		public CloudPlot( ISeries series, PlotStyle style ) {
			this.DataSource = series;
			DataSource.DataChanged += delegate( object sender, DataChangedArgs e ) { InvalidateVisual(); };
			this.PlotStyle = style;
		}

		protected override void OnRender( System.Windows.Media.DrawingContext cx ) {
			//Get data enumerator
			IEnumerator<Vector> data = PlotData.GetEnumerator();
			Vector offset = new Vector( -PlotData.Limits[0].Min, -PlotData.Limits[1].Min );
			Vector scale = new Vector( ActualWidth / PlotData.Limits[0].Range, ActualHeight / PlotData.Limits[1].Range );
			//move to first item
			data.MoveNext();
			Geometry Marker = PlotStyle.Marker.Clone();
			Marker.Transform = new ScaleTransform( PlotStyle.MarkerSize.value, PlotStyle.MarkerSize.value );
			Point old = (Point)Vector.PiecewiseMultiply( (data.Current + offset), scale );
			RaisePreIterate();
			while (data.MoveNext()) {
				Point p = (Point)Vector.PiecewiseMultiply( (data.Current + offset), scale );
				//Draw Marker
				cx.PushTransform( new TranslateTransform( p.X, p.Y ) );
				cx.DrawGeometry( PlotStyle.MarkerFill, PlotStyle.MarkerOutline, Marker );
				cx.Pop();
				old = p;
				RaisePlotIterate( data.Current );
			}
			RaisePostIterate();
		}
	}
}
